package com.uinnova.product.eam.init.cassso;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.binary.framework.bean.annotation.Comment;
import com.uinnova.product.eam.service.CasLoginService;
import com.uino.api.client.permission.IUserApiSvc;
import com.uino.bean.permission.base.SysUser;
import com.uino.bean.permission.business.UserInfo;
import com.uino.util.sys.CurrentUserGetter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 单点登录用户
 * @author: LiMG
 * @create: 2024-05-30 14:32:32
 **/
@Comment("国投IAM获取当前用户方法")
@Component
@Slf4j
@ConditionalOnProperty(name = "monet.login.loginMethod", havingValue = "guotouiam")
public class CASCurrentUser implements CurrentUserGetter {

    @Autowired
    private CasLoginService casLoginService;

    @Autowired
    private IUserApiSvc userApiSvc;

    @Override
    public SysUser getCurrentUserInfo() {
        String currentToken = getCurrentToken();
        return getCurrentUserInfo(currentToken);
    }

    @Override
    public SysUser getCurrentUserInfo(String currentToken) {
        if (currentToken == null) {
            return null;
        }
        Object userObj = getCurrentUser(currentToken);
        log.debug("guotouiam-getCurrentUserInfo:{}", userObj);
        if (userObj != null) {
            return (SysUser) userObj;
        } else {
            return null;
        }
    }

    @Override
    public String getCurrentToken() {
        // 已校验的用户无需再次校验
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            return null;
        }
        HttpServletRequest request;
        String ticket;
        request = attrs.getRequest();
        Object authorization = request.getAttribute("authorization");
        log.debug("guotouiam-getCurrentToken:{}", authorization);
        if (authorization != null) {
            ticket = "" + authorization;
        } else {
            return null;
        }
        return ticket;
    }

    private Object getCurrentUser(String currentToken) {
        Object userObj = null;
        try {
            userObj = casLoginService.getUser(currentToken);
            if(userObj == null){
                JSONObject userJson = casLoginService.getUserInfo(currentToken);
                if (userJson.getInteger("code") == 0) {
                    UserInfo userInfo = userApiSvc.getUserInfoByLoginCode(userJson.getString("account"));
                    userObj = userInfo;
                }
            }
        } catch (Exception e) {
            log.error("redis异常，用户信息从国投IAM取");
            //根据token从国投IAM取当前用户信息
            JSONObject userJson = casLoginService.getUserInfo(currentToken);
            if (userJson.getInteger("code") == 0) {
                UserInfo userInfo = userApiSvc.getUserInfoByLoginCode(userJson.getString("account"));
                userObj = userInfo;
            }
            log.error(JSON.toJSONString(e));
        }
        return userObj;
    }
}
